//
//  MemberPwEditViewController.m
//  LiteraryTune
//
//  Created by 陈雪丹 on 2018/2/4.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "MemberPwEditViewController.h"
#import "MemberCenterTopTableViewCell.h"
#import "MemberPwEditTableViewCell.h"

@interface MemberPwEditViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *myTableView;

@end

@implementation MemberPwEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"密码修改";
    [self buildUI];
}

- (void)buildUI {
    [self.view addSubview:self.myTableView];
}

#pragma mark - event response
- (void)confirmBtnClickAction {
    MemberPwEditTableViewCell *cell = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if (cell.originalTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入原始密码"];
    }else if (cell.pwTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入新密码"];
    }else if (cell.pwAgainTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请再次输入新密码"];
    }else if (![cell.pwTF.text isEqualToString:cell.pwAgainTF.text]) {
        [CXDNoticeView showNoticeViewWithString:@"两次密码输入不一致"];
    }else {
        [CXDNetHelp getRequestWithURLString:PwModifyURL parameters:@{@"oldpass":cell.originalTF.text,@"password":cell.pwTF.text,@"username":[CXDHelper getUserAccountName]} success:^(id  _Nullable responseObject) {
            if ([[responseObject objectForKey:@"zhuangtai"] isEqualToString:@"1"]) {
                [CXDNoticeView showNoticeViewWithString:[responseObject objectForKey:@"message"]];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [CXDNoticeView showNoticeViewWithString:[responseObject objectForKey:@"message"]];
            }
        } failure:^(NSError * _Nullable error) {
            
        }];
    }
}

#pragma mark - 懒加载
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MemberCenterTopTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MemberCenterTopTableViewCell class])];
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MemberPwEditTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MemberPwEditTableViewCell class])];
    }
    return _myTableView;
}

#pragma mark - 协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        MemberCenterTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MemberCenterTopTableViewCell class])];
//        return cell;
//    }
    MemberPwEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MemberPwEditTableViewCell class])];
    [cell.confirmBtn addTarget:self action:@selector(confirmBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        return 220;
//    }
    return 280;
}

@end
